package city.spring.modules.medicalstation.medicationRecord.service.impl;

import city.spring.modules.medicalstation.medicationRecord.entity.MedicationRecordEntity;
import city.spring.modules.medicalstation.medicationRecord.repository.MedicationRecordRepository;
import city.spring.modules.medicalstation.medicationRecord.service.MedicationRecordService;
import city.spring.modules.medicalstation.medicationRecord.vo.MedicationRecordVO;
import city.spring.modules.medicalstation.medicationRecordDrug.repository.MedicationRecordDrugExtRepository;
import city.spring.modules.medicalstation.medicationRecordDrug.vo.MedicationRecordDrugVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：用药记录
 *
 * @author 谢金婷
 * @date 2020-10-23 16:04:43
 */
@CacheConfig(cacheNames = {MedicationRecordServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class MedicationRecordServiceImpl extends ServiceImpl<MedicationRecordRepository, MedicationRecordEntity> implements MedicationRecordService {
    private final UserService userService;
    private final MedicationRecordDrugExtRepository medicationRecordDrugExtRepository;

    public MedicationRecordServiceImpl(UserService userService, MedicationRecordDrugExtRepository medicationRecordDrugExtRepository) {
        this.userService = userService;
        this.medicationRecordDrugExtRepository = medicationRecordDrugExtRepository;
    }

    @Override
    public void saveMedicationRecord(MedicationRecordEntity entity) {
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setRegister(userEntity.getId());
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMedicationRecord(MedicationRecordEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        // 修改用药日期和服药时间时 同时修改关联的药品的用药日期和服药时间
        List<MedicationRecordDrugVO> recordDrugs = medicationRecordDrugExtRepository.getRecordDrugs(entity.getId());
        recordDrugs.forEach(recordDrug -> {
            recordDrug.setRecorddate(entity.getDate());
            recordDrug.setMedicationTime(entity.getTime());
            medicationRecordDrugExtRepository.updateById(recordDrug);
        });
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMedicationRecord(String primaryKey) {
        removeById(primaryKey);
        // 删除包括的缴存药品关联表的信息
        List<MedicationRecordDrugVO> medicationRecordDrugVOS = medicationRecordDrugExtRepository.getRecordDrugs(primaryKey);
        System.out.println("medicationRecordDrugVOS" + medicationRecordDrugVOS);
        if (!medicationRecordDrugVOS.isEmpty()) {
            List<String> medicationRecordIds = medicationRecordDrugVOS.stream().map(MedicationRecordDrugVO::getId).collect(Collectors.toList());
            medicationRecordDrugExtRepository.deleteBatchIds(medicationRecordIds);
        }
    }

    @Override
    public void deleteMedicationRecord(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MedicationRecordVO> listPage(Page<? extends MedicationRecordEntity> page, Wrapper<? extends MedicationRecordEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}